//app.js
App({
  //全局变量
  globalData: {
    userInfo: null
  },
  util: require("utils/util.js"),
  siteInfo: require("siteinfo.js"),
  getHeaders: function (auth) {
    if (auth) {
      return {
        'content-type': 'application/json',
        'session-id': wx.getStorageSync('token'),
      }
    }
    return {
      'content-type': 'application/json',
    }
  },
  //普通用户封装请求函数 //默认为post 方法
  request: function (method, data, auth = false, request = false) {
    var promise = new Promise((resolve, reject) => {
      var that = this;
      var url = that.siteInfo.siteroot + '/api/' + method;
      //网络请求
      wx.request({
        url: url,
        data: data,
        method: request ? 'get' : 'post',
        header: this.getHeaders(auth),
        success: function (res) {
          //成功信息
          if (res.data.code == 1) {
            resolve(res.data);
          }
          //捕获错误
          if (res.data.code == 0) {
            reject(res.data);
          }
          //登录失效重新登录
          if (res.data.code == 2) {
            wx.showToast({
              title: '登陆中',
              icon: 'loading',
              duration: 1500,
              mask: true
            })
            that.util.login();
          }
        },
        fail: function (e) {
          wx.showModal({
            title: '温馨提示',
            content: '请稍后再试',
            showCancel: false
          })
        }
      })
    });
    return promise;
  },
  
})